package logic

import (
	"context"
	"fmt"

	"e4ting/internal/svc"
	"e4ting/pb/e4ting"
	// structpb "google.golang.org/protobuf/types/known/structpb"
	// "google.golang.org/protobuf/types/known/anypb"
	"github.com/zeromicro/go-zero/core/logx"
)

type ListTypeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListTypeLogic {
	return &ListTypeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListTypeLogic) ListType(in *e4ting.SearchRequest) (*e4ting.ManyTypeRes, error) {
	// todo: add your logic here and delete this line
	// size := max(in.Size, 20)
	// page := max(in.Page, 1)
	// limit,offset  := int(in.Size),int(in.Size * (max(1, in.Page) - 1))
	limit,offset,keyword := GetPage(in)
	items, count, err := l.svcCtx.TypeModel.Search(l.ctx, limit, offset)
	if err != nil {
		return &e4ting.ManyTypeRes{
			Code: 400,
			Err: err.Error(),
		}, nil
	}
	fmt.Printf("%d %d limit=%d,offset=%d,keyword=%s\n", count, len(items), limit,offset, keyword)

	return &e4ting.ManyTypeRes{
		Code: 200,
		Total: count,
		Data: ToTypeList(items),
	}, nil
}
